package cn.gov.sdcj.accountancy.rule

import cn.gov.sdcj.accountancy.entity.Item
import cn.gov.sdcj.accountancy.entity.VoucherList
import org.junit.Assert
import org.junit.Before
import org.junit.Test

/**
 * <p>Description: ScriptLoader 单元测试
 * </p>
 *
 * Created by laijie on 2015/3/27.
 */
class ScriptLoaderTest {
    List pzs;
    private File testFile;

    @Before
    public void setUp() throws URISyntaxException {
        pzs = [
                [bdgtType: '2', bdgtId: '2011701', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--行政运行', amt: 1234567.89,
                 payType : '01', payTypeName: '直接支付', srcId: '064', srcName: '暂存款', fexp: '测试用的1'],
                [bdgtType: '4', bdgtId: '2011702', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--一般行政管理事务', amt: 12345.67,
                 payType : '01', payTypeName: '直接支付', srcId: '100', srcName: '预算内', fexp: '测试用的2'],
                [bdgtType: '2', bdgtId: '2011703', bdgtName: '一般公共服务支出--质量技术监督与检验检疫事务--机关服务', amt: 123.45,
                 payType : '01', payTypeName: '授权支付', srcId: '051', srcName: '不知道', fexp: '测试用的3']
        ]
        URI file = ScriptLoaderTest.class.getResource("/rules.groovy").toURI();
        testFile = new File(file);
    }

    @Test
    public void testScriptLoader() throws IllegalAccessException, IOException, InstantiationException {
        ScriptLoader.load(testFile);
        Assert.assertNotNull(RuleManager.getRm().getRule("test"));
    }

    @Test
    public void testBuildTemplate() throws RuleException {
        VoucherList aT = new VoucherList();
        for (Object p : pzs) {
            Map m = (Map) p;
            VoucherList template = RuleManager.apply("test", m);
            Assert.assertNotNull(template);
            Assert.assertEquals("有两张凭证", 2, template.size());
        }
    }

    @Test
    public void testApplyTemplate() throws RuleException {
        VoucherList aT = new VoucherList();
        VoucherList template = RuleManager.apply("test", pzs);
        System.out.print(template);
    }

    @Test
    public void testItemEqual() {
        Map m1 = new HashMap();
        m1.put("className", "类别");
        m1.put("num", "123");
        m1.put("name", "名字");
        Map m2 = new HashMap();
        m2.put("className", "类别");
        m2.put("num", "123");
        m2.put("name", "名字");
        Item i1 = new Item();
        Item i2 = new Item();
        i1.add(m1);
        i2.add(m2);
        boolean b = i1.equals(i2);
        Assert.assertTrue(b);
    }

    @Test
    public void testGroovyClassLoader() throws IllegalAccessException, InstantiationException, IOException {
        ClassLoader parent = ScriptLoaderTest.class.getClassLoader();
        GroovyClassLoader loader = new GroovyClassLoader(parent);
        Class groovyClass = loader.parseClass(testFile);
        GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();

//        String name = "GroovyClassLoader";
//        groovyObject.setProperty("name", name);
//        Object[] argz = {};
//        Object object = groovyObject.invokeMethod("run", argz);
        groovyObject.run();
//        groovyObject."run"();
//        Closure dsl = (Closure)object;
//        dsl.call();
        Assert.assertNotNull(RuleManager.getRm().getRule("test"));
    }

}
